package com.controller.manager;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.service.DeviceService;

@Controller
@RequestMapping("/basicsData")
public class DeviceController {

	@Resource
	private DeviceService deviceService;

	@RequestMapping(value = "/toBasicsData")
	public ModelAndView toBasicsData(HttpServletRequest request) {
		String username = (String) request.getSession().getAttribute("username");
		if (username == null || "".equals(username)) {
			return new ModelAndView("/login");
		}
		return new ModelAndView("/manager/basicsdata");
	}

	@RequestMapping(value = "/addBasicsData", method = RequestMethod.POST)
	@ResponseBody
	public String addBasicsData(HttpServletRequest request, @RequestParam Map<String, String> paramsMap,
			@RequestParam(value = "imgfiles", required = false) MultipartFile[] imgfiles) {

		try {
			String contextPath = request.getScheme() // 当前链接使用的协议
					+ "://www.sijixiaoyuan.com"     // +request.getLocalAddr()// "www.baodaoapp.com"// 服务器地址  www.sijixiaoyuan.com
					+ ":" + request.getServerPort() // 端口号
					+ request.getContextPath(); // 应用名称，如果应用名称为
			paramsMap.put("contextPath", contextPath);
			String pathString = request.getSession().getServletContext().getRealPath("ewm");
			System.out.println("pathString:" + pathString);
			paramsMap.put("pathString", pathString);
			System.out.println("imgfiles---------->" + imgfiles);
			int result = deviceService.addBasicsData(paramsMap, imgfiles);
			if (result >= 0) {
				return "success";
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "error";
	}

	// 删除
	@RequestMapping(value = "/deleteBasicsData", method = RequestMethod.POST)
	@ResponseBody
	public String deleteBasicsData(@RequestParam Map<String, String> paramsMap) {
		try {
			int result = deviceService.deleteBasicsData(paramsMap);
			if (result >= 0) {
				return "success";
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "error";
	}

	@RequestMapping(value = "/updateBasicsDataInfo", method = RequestMethod.POST)
	@ResponseBody
	public String updateBasicsData(HttpServletRequest request, @RequestParam Map<String, String> paramsMap,
			@RequestParam(value = "imgfiles", required = false) MultipartFile[] imgfiles) {
		try {
			String contextPath = request.getScheme() // 当前链接使用的协议
					+ "://www.sijixiaoyuan.com"//+request.getLocalAddr() 服务器地址 www.sijixiaoyuan.com
					+ ":" + request.getServerPort() // 端口号
					+ request.getContextPath(); // 应用名称，如果应用名称为
			paramsMap.put("contextPath", contextPath);
			String pathString = request.getSession().getServletContext().getRealPath("ewm");
			System.out.println("pathString:" + pathString);
			paramsMap.put("pathString", pathString);
			int result = deviceService.updateBasicsData(paramsMap, imgfiles);
			if (result > 0) {
				return "success";
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "error";
	}

	// 查询
	@RequestMapping(value = "/getBasicsData", method = RequestMethod.GET)
	@ResponseBody
	public ModelAndView getBasicsData(HttpServletRequest request, @RequestParam Map<String, String> paramsMap) {
		try {
			Map<String, String> basicsData = deviceService.getBasicsData(paramsMap);
			request.setAttribute("basicsData", basicsData);
			return new ModelAndView("/manager/indexAll");
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	@RequestMapping(value = "/getBasicsDataByParam", method = RequestMethod.GET)
	@ResponseBody
	public List<Map<String, String>> getBasicsDataByName(@RequestParam Map<String, String> paramsMap) {
		try {
			List<Map<String, String>> list = deviceService.getBasicsDataByName(paramsMap);
			return list;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	@RequestMapping(value = "/getAllBasicsDataInfo", method = RequestMethod.GET)
	@ResponseBody
	public List<Map<String, String>> getAllBasicsDataInfo(@RequestParam Map<String, String> paramsMap) {
		try {
			List<Map<String, String>> list = deviceService.getAllBasicsDataInfo(paramsMap);
			return list;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	// 查询
	@RequestMapping(value = "/getQrcode", method = RequestMethod.GET)
	@ResponseBody
	public ModelAndView getQrcode(HttpServletRequest request, @RequestParam Map<String, String> paramsMap) {
		try {
			List<Map<String, String>> qrcode = deviceService.getQrcode(paramsMap);
			request.setAttribute("qrcode", qrcode);
			return new ModelAndView("/manager/allQrcode");
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

}
